package cn.edu.nju.newsFeed;

import cn.edu.nju.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class NewsFeedDetailActivity extends Activity {
	private ListItemFormatHelper helper;
	private int position; // the position of feedItem in the news feed list
	private NewsFeedItem feedItem;
	private ListView commentsList;
	private CommentListAdapter listAdapter;
	private FeedClickableSpan likeSpan;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_feed_item_detail);
        
        this.helper = new ListItemFormatHelper(this);
        
        Intent intent = getIntent();
        this.position = intent.getIntExtra(NewsFeedActivity.FEED_ITEM_POSITION, 0);
        this.feedItem = NewsFeedProvider.getInstance(this).getNewsFeedItem(position);
        this.feedItem.refresh(); // download latest data like comments
        
        // init widget
        initRefreshButton();
        initWriteCommentButton();
        initSharePost();
        initCommentsList();
	}
	
	@Override
	public void onResume(){
		super.onResume();
		feedItem.refresh();
		listAdapter.refresh();
		
		// roll back list to first item
		commentsList.setSelectionFromTop(0, 0);
		
		updateSharePost();
	}
	
	private void initRefreshButton(){
		Button refreshButton = (Button) findViewById(R.id.refresh_button);
		refreshButton.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				feedItem.refresh();
				
				listAdapter.refresh();
				// roll back list to first item
				commentsList.setSelectionFromTop(0, 0);
				
				updateSharePost();
			}
		});
	}
	
	private void initWriteCommentButton(){
		Button writeCommentButton = (Button) findViewById(R.id.write_comment_button);
		writeCommentButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				Intent intent = new Intent(NewsFeedDetailActivity.this, WritePostActivity.class);
				intent.putExtra(WritePostActivity.POST_MODE, WritePostActivity.COMMENT_MODE);
				intent.putExtra(NewsFeedActivity.FEED_ITEM_POSITION, position);
				startActivity(intent);
			}
		});
	}
	
	private void initSharePost(){
		initTextSpans();
		View postView = findViewById(R.id.share_post_item);
		
		postView.setBackgroundResource(R.color.lighter_blue);
		
		ImageView avatarView = (ImageView)postView.findViewById(R.id.avatar);
		helper.setAvatar(avatarView, feedItem);
		
		TextView contentView = (TextView)postView.findViewById(R.id.feed_item_text);
		helper.setContentText(contentView, feedItem, null, null);
		
		TextView timeView = (TextView)postView.findViewById(R.id.feed_time_text);
		helper.setTimeText(timeView, feedItem);
		
		ImageView previewImageView = (ImageView) postView.findViewById(R.id.feed_item_image_preview);
		previewImageView.setTag(R.id.VIEW_POSITION_TAG, position);
		helper.setPreviewImage(previewImageView, feedItem, true);
		
		TextView commentsNumberView = (TextView)postView.findViewById(R.id.comments_number_text);
		helper.setCommentNumberText(commentsNumberView, feedItem, null);
		
		TextView likeNumberView = (TextView)postView.findViewById(R.id.like_number_text);
		likeNumberView.setMovementMethod(LinkMovementMethod.getInstance());
		helper.setLikeNumberText(likeNumberView, feedItem, likeSpan);
		
	}
	
	private void initCommentsList(){
		commentsList = (ListView)findViewById(R.id.comments_list);
		listAdapter = new CommentListAdapter(this, feedItem);
		commentsList.setAdapter(listAdapter);
	}
	
	private void initTextSpans(){
		likeSpan = new FeedClickableSpan(this) {			
			@Override
			public void onClick(View widget) {
				feedItem.addOneLike();
				helper.setLikeNumberText((TextView)widget, feedItem, likeSpan);
			}
		};
	}
	
	private void updateSharePost(){
		View postView = findViewById(R.id.share_post_item);
		
		TextView timeView = (TextView)postView.findViewById(R.id.feed_time_text);
		helper.setTimeText(timeView, feedItem);
		
		TextView commentsNumberView = (TextView)postView.findViewById(R.id.comments_number_text);
		helper.setCommentNumberText(commentsNumberView, feedItem, null);
		
		TextView likeNumberView = (TextView)postView.findViewById(R.id.like_number_text);
		helper.setLikeNumberText(likeNumberView, feedItem, likeSpan);
	}
}
